home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Text Editor / Source / TextEditorImaging.cpp < prev    next >
Encoding:
Text File  |  1995-12-13  |  29.3 KB  |  1,123 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  
  3.     File:            TextEditorImaging.cpp
  4.     
  5.     Description:    TextEditor's imaging protocol methods.
  6.     
  7.     Written by:        Steve Smith
  8.     
  9.     Copyright:        © 1994 - 1995 by Apple Computer, Inc., all rights reserved.
  10.     
  11. ------------------------------------------------------------------------------*/
  12.  
  13. // Notification that this is a SOM source file
  14. #define SampleCode_TextEditor_Class_Prototypes
  15. // define underscore (_) field names
  16. #define VARIABLE_MACROS
  17.  
  18. #ifndef _COMPILERDEFS_
  19. #include "CompDefs.h"
  20. #endif
  21.  
  22. // -- OpenDoc Utilities --
  23.  
  24. #ifndef _EXCEPT_
  25. // Exceptions define several important macros (e.g. CHECKENV)
  26. // which are used in the SOM method dispatch glue. If Except.h
  27. // is not included early enough, exceptions may not be thrown
  28. // correctly when returning from a SOM method with "ev" parameter set.
  29. #include <Except.h>
  30. #endif
  31.  
  32. // -- TextEditor Includes
  33.  
  34. #ifndef _TEXTEDITORDEF_
  35. #include "TextEditorDef.h"
  36. #endif
  37.  
  38. #ifndef SOM_SampleCode_TextEditor_xih
  39. #include "TextEditor.xih"
  40. #endif
  41.  
  42. #ifndef _TEXTEDITORUTILS_
  43. #include "TextEditorUtils.h"
  44. #endif
  45.  
  46. #ifndef _TEXTEDITORGLOBALS_
  47. #include "TextEditorGlobals.h"
  48. #endif
  49.  
  50. #ifndef _STDTEXTPROPERTIES_
  51. #include "StdTextProps.h"
  52. #endif
  53.  
  54. // -- OpenDoc Includes --
  55.  
  56. #ifndef _ODTYPES_
  57. #include <ODTypes.h>
  58. #endif
  59.  
  60. #ifndef SOM_ODCanvas_xh
  61. #include <Canvas.xh>
  62. #endif
  63.  
  64. #ifndef SOM_ODPart_xh
  65. #include <Part.xh>
  66. #endif
  67.  
  68. #ifndef SOM_ODFacet_xh
  69. #include <Facet.xh>
  70. #endif
  71.  
  72. #ifndef SOM_ODTransform_xh
  73. #include <Trnsform.xh>
  74. #endif
  75.  
  76. #ifndef SOM_ODFrame_xh
  77. #include <Frame.xh>
  78. #endif
  79.  
  80. // -- OpenDoc Utilities --
  81.  
  82. #ifndef _FOCUSLIB_
  83. #include <FocusLib.h>
  84. #endif
  85.  
  86. #ifndef _ODUTILS_
  87. #include <ODUtils.h>
  88. #endif
  89.  
  90. #ifndef _TEMPOBJ_
  91. #include <TempObj.h>
  92. #endif
  93.  
  94. #ifndef _USERSRCM_
  95. #include <UseRsrcM.h>
  96. #endif
  97.  
  98. // -- Macintosh Includes --
  99.  
  100. #ifndef __QUICKDRAW__
  101. #include <Quickdraw.h>
  102. #endif
  103.  
  104. // -- Textension Includes --
  105.  
  106. #ifndef _Textension_
  107. #include "Textension.h"
  108. #endif
  109.  
  110. #ifndef _QDTextRun_
  111. #include "QDTextRun.h"
  112. #endif
  113.  
  114. #ifndef _RulerObject_
  115. #include "RulerObject.h"
  116. #endif
  117.  
  118. #ifndef _Frames_
  119. #include <Frames.h>
  120. #endif
  121.  
  122. #ifndef _LinkedFrames_
  123. #include "LinkedFrames.h"
  124. #endif
  125.  
  126.  
  127. //------------------------------------------------------------------------------
  128. // Method:        GenerateThumbnail
  129. // Origin:        TextEditor
  130. //
  131. // Description:    This method is called by the part to generate a thumbnail view
  132. //                from the current content.
  133. //------------------------------------------------------------------------------
  134. #pragma segment TextEditorImaging
  135.  
  136. SOM_Scope    void
  137. SOMLINK        TextEditor__GenerateThumbnail
  138.              (
  139.                 SampleCode_TextEditor*        somSelf,
  140.                 Environment*                ev,
  141.                 ODFrame*                    frame
  142.             )
  143. {
  144. //    SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  145.     SOMMethodDebug("TextEditor","GenerateThumbnail");
  146.  
  147.     SOM_TRY
  148.     
  149.         // In cases where a part has been instantiated from scratch and has no
  150.         // content (yet), it is appropriate to display a PICT or some graphic
  151.         // in place of a "real" thumbnail.
  152.     
  153.     //$$$$$ if not content
  154.         LoadThumbnail(ev, &gGlobals->fThumbnail);
  155.     
  156.         // If we were unable to load the PICT resource for whatever reason
  157.         // we will default back to a "frame" view and throw the Resource
  158.         // Manager error as an exception.
  159.         if ( gGlobals->fThumbnail == kODNULL )
  160.         {
  161.             frame->ChangeViewType(ev, gGlobals->fFrameView);
  162.             
  163.             // There is a bug in ResError, when resources are not found,
  164.             // which may cause noErr to be returned. If that is the case,
  165.             // we throw resNotFound.
  166.             THROW_IF_ERROR(ResError());
  167.             THROW(resNotFound);
  168.         }
  169.     
  170.     SOM_CATCH_ALL
  171.     SOM_ENDTRY
  172. }
  173.  
  174. //------------------------------------------------------------------------------
  175. // Method:        FacetAdded
  176. // Origin:        TextEditor
  177. //
  178. // Description:    This method is called when any part adds a facet to
  179. //                one of our display frames.
  180. //
  181. //                The part calls ViewTypeChanged to load the appropriate
  182. //                resource for display in this facet and then activates
  183. //                the frame if we are the root part of an active window.
  184. //------------------------------------------------------------------------------
  185. #pragma segment TextEditorImaging
  186.  
  187. SOM_Scope    void
  188. SOMLINK        TextEditor__FacetAdded
  189.             (
  190.                 SampleCode_TextEditor*        somSelf,
  191.                 Environment*                ev,
  192.                 ODFacet*                    facet
  193.             )
  194. {
  195.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  196.     SOMMethodDebug("TextEditor","FacetAdded");
  197.  
  198.     SOM_TRY
  199.  
  200.         ODFrame*    frame = facet->GetFrame(ev);
  201.  
  202.         // We need to do nothing special for the ruler frame/facet.
  203.         if ( frame->GetPresentation(ev) == gGlobals->fRulerPresentation )
  204.             return;
  205.     
  206.         CFrameInfo*    frameInfo = (CFrameInfo*) frame->GetPartInfo(ev);
  207.     
  208.         CFocus initiateDrawing(ev, facet);
  209.     
  210.         // We need to Initialize Textension based on the scope of part
  211.         // this instance is, either "root" or "embedded". We do it here
  212.         // because this is the first place where it is possible to determine
  213.         // the part scope when an existing document is opened.
  214.         
  215.         if ( !_fTextensionInitialized )
  216.         {
  217.             somSelf->InitializeTextension(ev, frame->IsRoot(ev));
  218.             
  219.             _fTextension->SetTextPort(facet->GetCanvas(ev)->GetQDPort(ev));
  220.     
  221.             // Read the contents of the document.
  222.             somSelf->InternalizeContent(ev, somSelf->GetStorageUnit(ev), kODNULL);
  223.             
  224.             // After internalizing the content, we examine the ruler object to
  225.             // determine the justification of the saved document.
  226.             long length;
  227.             char just;
  228.             
  229.             const CRulerObject* ruler = _fTextension->GetNextRuler((long)0, &length);
  230.             if ( ruler )
  231.                 ruler->GetAttributeValue(kJustAttr, &just);
  232.     /*        else
  233.             {
  234.                 somSelf->DoDialogBox(ev, frame, kErrorBoxID, kErrTxCreateObjectFailed);
  235.                 
  236.                 //$$$$$ go through the protocol of removing this frame!
  237.             }
  238.     */
  239.             
  240.             switch ( just )
  241.             {
  242.                 case kLeftJust:        _fTextAlignment = left;                 break;
  243.                 case kCenterJust:    _fTextAlignment = center;             break;
  244.                 case kRightJust:    _fTextAlignment = right;             break;
  245.                 case kFullJust:        _fTextAlignment = fulljustification; break;
  246.                 default:
  247.                     _fTextAlignment = left;
  248.             }
  249.         }
  250.         
  251.         if ( frame->IsRoot(ev) )        
  252.         {
  253.             // Calculate the size of the ViewRect for this root frame.
  254.             somSelf->CalcViewRect(ev, frame);
  255.         }
  256.         else
  257.         {
  258.             // Calculate the usable area for this embedded frame.
  259.             TLongRect textBounds;
  260.             ODRect    bounds;
  261.             
  262.             TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);
  263.             frameShape->GetBoundingBox(ev, &bounds);
  264.     
  265.             textBounds.top        = FixedToInt(bounds.top);
  266.             textBounds.left        = FixedToInt(bounds.left);
  267.             textBounds.bottom    = FixedToInt(bounds.bottom);
  268.             textBounds.right    = FixedToInt(bounds.right);
  269.     
  270.             CVariableSizeFrames* frames = 
  271.                 (CVariableSizeFrames*) _fTextension->GetFramesHandler();
  272.             
  273.             CDisplayChanges changes;
  274.             frames->AddFrame(0, textBounds, &changes);
  275.             
  276.             (void) _fTextension->DisplayChanged(changes);
  277.         }
  278.     
  279.         // Set the text bounds and margins for the frame
  280.         somSelf->CalcTextBounds(ev, frame);
  281.     
  282.         TOffsetRange selRange(_fTextension->CountCharsBytes(), _fTextension->CountCharsBytes());
  283.         _fTextension->SetSelectionRange(selRange);
  284.     
  285.         somSelf->ViewTypeChanged(ev, frame);
  286.             // We call this to prepare our part for being displayed in one of
  287.             // the four standard views: Large Icon, Small Icon, Frame, and
  288.             // Thumbnail.
  289.             
  290.         if ( frame->IsRoot(ev) )
  291.         {
  292.             // If a root facet is being added, the frame should be activated when the
  293.             // window is "selected". This prevents OpenDoc from getting into an
  294.             // ambiguous state of no part having the selection focus.
  295.             frameInfo->SetFrameReactivate(kODTrue);
  296.             // We set the active facet even though the window may not become active
  297.             // because Textension requires a valid facet for drawing.
  298.             frameInfo->SetActiveFacet(facet);
  299.                     
  300.             // Add scroll bars
  301.             
  302.             ODRect        bbox;
  303.             Rect        frameRect;
  304.             Rect        scrollBarRect;
  305.             
  306.             TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);
  307.             frameShape->GetBoundingBox(ev, &bbox);
  308.     
  309.             FixedToIntRect(bbox, frameRect);
  310.                 
  311.             scrollBarRect = frameRect;
  312.             scrollBarRect.left = scrollBarRect.right - 15;
  313.             scrollBarRect.right += 1;
  314.             scrollBarRect.top -= 1;
  315.             scrollBarRect.bottom -= 14;
  316.             _fVScrollbar = NewControl(facet->GetCanvas(ev)->GetQDPort(ev),
  317.                                       &scrollBarRect, "\p", true, 
  318.                                       0/*val*/, 0/*min*/, 0/*max*/, 
  319.                                       scrollBarProc, 0);
  320.             
  321.             scrollBarRect = frameRect;
  322.             scrollBarRect.left -= 1;
  323.             scrollBarRect.right -= 14;
  324.             scrollBarRect.top = scrollBarRect.bottom - 15;
  325.             scrollBarRect.bottom += 1;
  326.             _fHScrollbar = NewControl(facet->GetCanvas(ev)->GetQDPort(ev),
  327.                                       &scrollBarRect, "\p", true, 
  328.                                       0/*val*/, 0/*min*/, 0/*max*/, 
  329.                                       scrollBarProc, 0);
  330.             
  331.             somSelf->AdjustScrollbars(ev, frame);
  332.         }
  333.         else
  334.         {
  335.             // If a frame had all of its facets removed, the frame would have
  336.             // hidden any of its part windows. If the frame becomes visible again,
  337.             // by having a facet added to it, we will "show" the part window for
  338.             // the frame.
  339.             
  340.             if ( CountFacets(ev, frame) == 1 )
  341.             {
  342.                 TempODWindow window = frameInfo->AcquirePartWindow(ev);
  343.                 if ( window ) 
  344.                     window->Show(ev);
  345.             }
  346.         }
  347.     
  348.     SOM_CATCH_ALL
  349.     SOM_ENDTRY
  350. }
  351.  
  352. //------------------------------------------------------------------------------
  353. // Method:        FacetRemoved
  354. // Origin:        ODPart
  355. //
  356. // Description:    This method is called when any part removes a facet
  357. //                from one of our display frames.
  358. //
  359. //                The part just removes the "active" note from the
  360. //                appropriate display frame if necessary since this
  361. //                facet will not be available, nor active, again.
  362. //------------------------------------------------------------------------------
  363. #pragma segment TextEditorImaging
  364.  
  365. SOM_Scope    void
  366. SOMLINK        TextEditor__FacetRemoved
  367.             (
  368.                 SampleCode_TextEditor*        somSelf,
  369.                 Environment*                ev,
  370.                 ODFacet*                    facet
  371.             )
  372. {
  373.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  374.     SOMMethodDebug("TextEditor","FacetRemoved");
  375.  
  376.     SOM_TRY
  377.     
  378.         ODFrame* frame = facet->GetFrame(ev);
  379.  
  380.         // We need to do nothing special for the ruler frame/facet.
  381.         if ( frame->GetPresentation(ev) != gGlobals->fRulerPresentation )
  382.         {
  383.             TempODFrame    containingFrame = frame->AcquireContainingFrame(ev);
  384.             CFrameInfo* frameInfo = (CFrameInfo*) frame->GetPartInfo(ev);
  385.             
  386.             // If the facet was the active facet, it can no longer be.
  387.             if (frameInfo->GetActiveFacet() == facet)
  388.                 frameInfo->SetActiveFacet(kODNULL);
  389.         
  390.             // If a frame has all of its facets removed, the frame becomes
  391.             // "hidden". If the frame a part window, the part window should also
  392.             // be hidden.
  393.             
  394.             if ( CountFacets(ev, frame) == 0 && containingFrame == kODNULL )
  395.             {
  396.                 TempODWindow window = frameInfo->AcquirePartWindow(ev);
  397.                 if ( window ) 
  398.                     window->Hide(ev);
  399.             }
  400.         }
  401.                 
  402.     SOM_CATCH_ALL
  403.     SOM_ENDTRY
  404. }
  405.  
  406. //------------------------------------------------------------------------------
  407. // Method:        Draw
  408. // Origin:        ODPart
  409. //
  410. // Description:    This method is called when a facet of a part's display
  411. //                frame intersects the invalidated portion of an OpenDoc
  412. //                window. The invalidShape parameter passed in is the
  413. //                portion of the facet which has been invalidated.
  414. //
  415. //                The part sets up the drawing environment using a
  416. //                C++ helper class (CFocus) and then calls the
  417. //                appropriate drawing method based on the frame's
  418. //                viewType.
  419. //------------------------------------------------------------------------------
  420. #pragma segment TextEditorImaging
  421.  
  422. SOM_Scope    void
  423. SOMLINK        TextEditor__Draw
  424.             (
  425.                 SampleCode_TextEditor*        somSelf,
  426.                 Environment*                ev,
  427.                 ODFacet*                    facet,
  428.                 ODShape*                    invalidShape
  429.             )
  430. {
  431.     SOMMethodDebug("TextEditor","Draw");
  432.  
  433.     SOM_TRY
  434.     
  435.         // Focus the port and origin for drawing in our facet.
  436.         // Note that this instance of the CFocusDrawingEnv class
  437.         // is being allocated on the stack. When the execution
  438.         // leaves the scope of this method, the destructor (which
  439.         // cleans up the drawing environment) is automatically
  440.         // called.
  441.         CFocus initiateDrawing(ev, facet);
  442.         
  443.         ODTypeToken view = facet->GetFrame(ev)->GetViewType(ev);
  444.         
  445.         if ( view == gGlobals->fLargeIconView || view == gGlobals->fSmallIconView )
  446.             somSelf->DrawIconView(ev, facet);
  447.         else if ( view == gGlobals->fThumbnailView )
  448.             somSelf->DrawThumbnailView(ev, facet);
  449.         else
  450.             somSelf->DrawFrameView(ev, facet);
  451.  
  452.     SOM_CATCH_ALL
  453.     SOM_ENDTRY
  454. }
  455.  
  456. //------------------------------------------------------------------------------
  457. // Method:        DrawFrameView
  458. // Origin:        TextEditor
  459. //
  460. // Description:    This method is called by the part when the frame being
  461. //                drawn is in "frame" view.
  462. //------------------------------------------------------------------------------
  463. #pragma segment TextEditorImaging
  464.  
  465. SOM_Scope    void
  466. SOMLINK        TextEditor__DrawFrameView
  467.              (
  468.                 SampleCode_TextEditor*        somSelf,
  469.                 Environment*                ev,
  470.                 ODFacet*                    facet
  471.             )
  472. {
  473.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  474.     SOMMethodDebug("TextEditor","DrawFrameView");
  475.  
  476.     SOM_TRY
  477.     
  478.         GrafPtr thePort;
  479.         GetPort(&thePort);
  480.         EraseRect(&thePort->portRect);
  481.         
  482.         ODFrame* frame = facet->GetFrame(ev);
  483.     
  484.         if ( frame->GetPresentation(ev) == gGlobals->fMainPresentation )
  485.         {
  486.             ODRect        bbox;
  487.             Rect        updateRect;
  488.             
  489.             // Draw the scrollbars, if we have them.
  490.             if ( frame->IsRoot(ev) )
  491.             {
  492.                 Draw1Control(_fVScrollbar);
  493.                 Draw1Control(_fHScrollbar);
  494.             }
  495.         
  496.             TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);
  497.             frameShape->GetBoundingBox(ev, &bbox);
  498.             
  499.             FixedToIntRect(bbox, updateRect);
  500.             
  501.             // Subtract the scrollbars from the update area, if we have them.
  502.             if ( frame->IsRoot(ev) )
  503.             {
  504.                 updateRect.right -= 15;
  505.                 updateRect.bottom += 15;
  506.             }
  507.         
  508.             _fTextension->SetTextPort(facet->GetCanvas(ev)->GetQDPort(ev));
  509.             _fTextension->Draw(updateRect, kNoDrawFlags);
  510.             
  511.             // Frame the text bounds to make the margins visible.    
  512.             if ( frame->IsRoot(ev) )
  513.                 somSelf->DrawPageBounds(ev);
  514.         }
  515.         else
  516.         {
  517.             CUsingLibraryResources res;
  518.  
  519.             PicHandle pict = GetPicture(kRulerPictResID);
  520.             
  521.             if (pict)
  522.             {
  523.                 Rect pictRect = (**pict).picFrame;
  524.                 OffsetRect(&pictRect, -pictRect.left, -pictRect.top);
  525.                 
  526.                 DrawPicture(pict, &pictRect);
  527.             }
  528.         }
  529.     
  530.     SOM_CATCH_ALL
  531.     SOM_ENDTRY
  532. }
  533.  
  534. //------------------------------------------------------------------------------
  535. // Method:        DrawPageBounds
  536. // Origin:        TextEditor
  537. //
  538. // Description:    This method is called by the part when the page bounds need to
  539. //                be drawn.
  540. //------------------------------------------------------------------------------
  541. #pragma segment TextEditorImaging
  542.  
  543. SOM_Scope    void
  544. SOMLINK        TextEditor__DrawPageBounds
  545.              (
  546.                 SampleCode_TextEditor*        somSelf,
  547.                 Environment*                ev
  548.             )
  549. {
  550.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  551.     SOMMethodDebug("TextEditor","DrawPageBounds");
  552.  
  553.     SOM_TRY
  554.  
  555.         Rect        pageRect;
  556.         Rect        viewRect;
  557.         RgnHandle    saveClip;
  558.         
  559.         PenNormal();
  560.         PenPat(&ODQDGlobals.gray);
  561.         
  562.         _fTextension->GetViewRect(&viewRect);
  563.         
  564.         GetClip(saveClip = NewRgn());
  565.         ClipRect(&viewRect);
  566.     
  567.         CFrames* frames = _fTextension->GetFramesHandler();
  568.         
  569.         for (int i = 0; i < frames->GetCountFrames(); i++)
  570.         {
  571.             frames->GetTextBounds(i, &pageRect);
  572.             
  573.             InsetRect(&pageRect, -3, -3);
  574.             FrameRect(&pageRect);
  575.         }
  576.         
  577.         PenNormal();
  578.     
  579.         SetClip(saveClip);
  580.         DisposeRgn(saveClip);
  581.         
  582.     SOM_CATCH_ALL
  583.     SOM_ENDTRY
  584. }
  585.  
  586. //------------------------------------------------------------------------------
  587. // Method:        DrawIconView
  588. // Origin:        TextEditor
  589. //
  590. // Description:    This method is called by the part when the frame being
  591. //                drawn is in "standard icon" view.
  592. //
  593. //                The part uses the Icon Utilities toolbox manager to
  594. //                aid in drawing icons in active windows. The Guidelines
  595. //                require a different appearance for selected icons in
  596. //                inactive windows, which we do manually.
  597. //------------------------------------------------------------------------------
  598. #pragma segment TextEditorImaging
  599.  
  600. SOM_Scope    void
  601. SOMLINK        TextEditor__DrawIconView
  602.              (
  603.                 SampleCode_TextEditor*        somSelf,
  604.                 Environment*                ev,
  605.                 ODFacet*                    facet
  606.             )
  607. {
  608.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  609.     SOMMethodDebug("TextEditor","DrawIconView");
  610.  
  611.     SOM_TRY
  612.     
  613.         IconTransformType     transformType    = ttNone;        
  614.         ODFrame*            frame            = facet->GetFrame(ev);
  615.         ODTypeToken            viewType        = frame->GetViewType(ev);
  616.         CFrameInfo*            frameInfo         = (CFrameInfo*) frame->GetPartInfo(ev);
  617.         
  618.         // Check to see if the facet is selected
  619.         if ( facet->GetHighlight(ev) == kODFullHighlight )
  620.             transformType = ttSelected;
  621.         
  622.         // Check to see if the frame has been opened into a part window.
  623.         TempODWindow window = frameInfo->AcquirePartWindow(ev);
  624.         if ( window && window->IsShown(ev) )
  625.             transformType |= ttOpen;
  626.         
  627.         // Draw the icon.
  628.         Rect iconRect;
  629.         SetRect(&iconRect, 0, 0, 
  630.                 (viewType == gGlobals->fLargeIconView) ? kODLargeIconSize : kODSmallIconSize,
  631.                 (viewType == gGlobals->fLargeIconView) ? kODLargeIconSize : kODSmallIconSize);
  632.         
  633.         CUsingLibraryResources res;
  634.         PlotIconID(&iconRect, atAbsoluteCenter, transformType, kBaseResourceID);
  635.         
  636.     SOM_CATCH_ALL
  637.     SOM_ENDTRY
  638. }
  639.  
  640. //------------------------------------------------------------------------------
  641. // Method:        DrawThumbnailView
  642. // Origin:        TextEditor
  643. //
  644. // Description:    This method is called by the part when the frame being
  645. //                drawn is in "thumbnail" view.
  646. //
  647. //                The part uses a picture for its thumbnail view because
  648. //                it has no intrinsic content. A picture resource is
  649. //                probably not sufficient for parts with real content.
  650. //------------------------------------------------------------------------------
  651. #pragma segment TextEditorImaging
  652.  
  653. SOM_Scope    void
  654. SOMLINK        TextEditor__DrawThumbnailView
  655.              (
  656.                 SampleCode_TextEditor*        somSelf,
  657.                 Environment*                ev,
  658.                 ODFacet*                    facet
  659.             )
  660. {
  661.     SOMMethodDebug("TextEditor","DrawThumbnailView");
  662.  
  663.     SOM_TRY
  664.     
  665.         LoadThumbnail(ev, &gGlobals->fThumbnail);
  666.         
  667.         Rect bounds = (**(PicHandle) gGlobals->fThumbnail).picFrame;
  668.         
  669.         OffsetRect(&bounds, -bounds.left, -bounds.top);
  670.         DrawPicture((PicHandle) gGlobals->fThumbnail, &bounds);
  671.         
  672.     SOM_CATCH_ALL
  673.     SOM_ENDTRY
  674. }
  675.  
  676. //------------------------------------------------------------------------------
  677. // Method:        GeometryChanged
  678. // Origin:        ODPart
  679. //
  680. // Description:    This method is called when the ExternalTransform or
  681. //                ClipShape of a facet on one this part's display frames
  682. //                changes.
  683. //------------------------------------------------------------------------------
  684. #pragma segment TextEditorImaging
  685.  
  686. SOM_Scope    void
  687. SOMLINK        TextEditor__GeometryChanged
  688.             (
  689.                 SampleCode_TextEditor*        somSelf,
  690.                 Environment*                ev,
  691.                 ODFacet*                    facet,
  692.                 ODBoolean                    clipShapeChanged,
  693.                 ODBoolean                    externalTransformChanged
  694.             )
  695. {
  696.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  697.     SOMMethodDebug("TextEditor","GeometryChanged");
  698.  
  699.     SOM_TRY
  700.     
  701.         // Typically, when the frameshape changes, you want to invalidate only the "new"
  702.         // area. However, TextEditor's display changes based on the frame shape so we
  703.         // must invalidate the entire area.
  704.         
  705.         if ( clipShapeChanged )
  706.             // Specifying kODNULL means to invalidate the clipShape (which was
  707.             // calculated from the usedShape).
  708.             facet->Invalidate(ev, kODNULL, kODNULL);
  709.     
  710.     SOM_CATCH_ALL
  711.     SOM_ENDTRY
  712. }
  713.  
  714. //------------------------------------------------------------------------------
  715. // Method:        HighlightChanged
  716. // Origin:        ODPart
  717. //
  718. // Description:    This method is called when a facet....
  719. //------------------------------------------------------------------------------
  720. #pragma segment TextEditorImaging
  721.  
  722. SOM_Scope    void
  723. SOMLINK        TextEditor__HighlightChanged
  724.             (
  725.                 SampleCode_TextEditor*        somSelf,
  726.                 Environment*                ev,
  727.                 ODFacet*                    facet
  728.             )
  729. {
  730. //    SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  731.     SOMMethodDebug("TextEditor","HighlightChanged");
  732.     
  733.     SOM_TRY
  734.     
  735.         ODFrame* frame = facet->GetFrame(ev);
  736.         
  737.         // The frame view has no "special" drawing characteristics
  738.         // when opened or selected, so we don't need to update our
  739.         // content.
  740.         
  741.         //$$$$$ shouldn't we change Textension from active to inactive
  742.         // hilighting?
  743.         
  744.         if ( frame->GetViewType(ev) != gGlobals->fFrameView )
  745.             frame->Invalidate(ev, kODNULL, kODNULL);
  746.     
  747.     SOM_CATCH_ALL
  748.     SOM_ENDTRY
  749. }
  750.  
  751. //------------------------------------------------------------------------------
  752. // Method:        CalcViewRect
  753. // Origin:        TextEditor
  754. //
  755. // Description:    This method is called when a facet....
  756. //
  757. // Note:        This routine only works on root frames.
  758. //------------------------------------------------------------------------------
  759. #pragma segment TextEditorImaging
  760.  
  761. SOM_Scope    void
  762. SOMLINK        TextEditor__CalcViewRect
  763.             (
  764.                 SampleCode_TextEditor*        somSelf,
  765.                 Environment*                ev,
  766.                 ODFrame*                    frame
  767.             )
  768. {
  769.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  770.     SOMMethodDebug("TextEditor","CalcViewRect");
  771.  
  772.     SOM_TRY
  773.  
  774.         WASSERT(frame && frame->IsRoot(ev));
  775.     
  776.         ODRect        bbox;
  777.         Rect        viewRect;
  778.         CFrameInfo*    frameInfo = (CFrameInfo*) frame->GetPartInfo(ev);
  779.     
  780.         TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);        
  781.         frameShape->GetBoundingBox(ev, &bbox);
  782.         
  783.         FixedToIntRect(bbox, viewRect);
  784.         
  785.         if ( frame->IsRoot(ev) )
  786.         {
  787.             viewRect.right  -= 15;
  788.             viewRect.bottom -= 15;
  789.         }
  790.         
  791.         _fTextension->SetViewRect(viewRect);
  792.         
  793.     SOM_CATCH_ALL
  794.     SOM_ENDTRY
  795. }
  796.  
  797. //------------------------------------------------------------------------------
  798. // Method:        CalcTextBounds
  799. // Origin:        TextEditor
  800. //
  801. // Description:    This method is called when a facet....
  802. //------------------------------------------------------------------------------
  803. #pragma segment TextEditorImaging
  804.  
  805. SOM_Scope    void
  806. SOMLINK        TextEditor__CalcTextBounds
  807.             (
  808.                 SampleCode_TextEditor*        somSelf,
  809.                 Environment*                ev,
  810.                 ODFrame*                    frame
  811.             )
  812. {
  813.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  814.     SOMMethodDebug("TextEditor","CalcTextBounds");
  815.  
  816.     SOM_TRY
  817.     
  818.         Rect textBounds;
  819.         
  820.         if ( frame->IsRoot(ev) )
  821.         {
  822.             textBounds = _fPaperSize;
  823.             
  824.             textBounds.top         += _fPageMargins.top;
  825.             textBounds.left     += _fPageMargins.left;
  826.             textBounds.bottom     -= _fPageMargins.bottom;
  827.             textBounds.right     -= _fPageMargins.right;
  828.         }
  829.         else
  830.         {
  831.             ODRect bbox;
  832.             TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);        
  833.             frameShape->GetBoundingBox(ev, &bbox);
  834.             
  835.             FixedToIntRect(bbox, textBounds);
  836.         }
  837.             
  838.         CFrames* frames = _fTextension->GetFramesHandler();
  839.         TLongPoint size((textBounds.right-textBounds.left), (textBounds.bottom-textBounds.top));
  840.             
  841.         CDisplayChanges displayChanges;
  842.         
  843.         frames->SetTextBoundsSize(size, &displayChanges, 0);
  844.         frames->SetFramesOrigin(0,0);
  845.         frames->SetFramesMargins(_fPageMargins);
  846.     
  847.         short action = _fTextension->DisplayChanged(displayChanges);
  848.         
  849.         if ( action & kRedrawAll )
  850.             frame->Invalidate(ev, kODNULL, kODNULL);
  851.     
  852.     SOM_CATCH_ALL
  853.     SOM_ENDTRY
  854. }
  855.  
  856. //------------------------------------------------------------------------------
  857. // Method:        AdjustScrollbars
  858. // Origin:        TextEditor
  859. //
  860. // Description:    This method is called when a facet....
  861. //------------------------------------------------------------------------------
  862. #pragma segment TextEditorImaging
  863.  
  864. SOM_Scope    void
  865. SOMLINK        TextEditor__AdjustScrollbars
  866.             (
  867.                 SampleCode_TextEditor*        somSelf,
  868.                 Environment*                ev,
  869.                 ODFrame*                    frame
  870.             )
  871. {
  872.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  873.     SOMMethodDebug("TextEditor","AdjustScrollbars");
  874.  
  875.     SOM_TRY
  876.     
  877.         if ( frame->IsRoot(ev) )
  878.         {
  879.             Rect viewRect;
  880.             _fTextension->GetViewRect(&viewRect);
  881.         
  882.             TLongPoint scrolledVals;
  883.             _fTextension->GetScrolledValues(&scrolledVals);
  884.     
  885.             ODSLong max = _fTextension->GetTotalHeight() 
  886.                             - (viewRect.bottom - viewRect.top);
  887.             if ( max < 0 )
  888.             {
  889.                 // Deactivate the scrollbar.
  890.                 HiliteControl( _fVScrollbar, 255);
  891.             }
  892.             else
  893.             {
  894.                 HiliteControl(_fVScrollbar, 0);
  895.                 SetControlMaximum(_fVScrollbar, max);
  896.                 SetControlValue(_fVScrollbar, scrolledVals.v);
  897.             }
  898.                 
  899.             max = _fTextension->GetTotalWidth() - (viewRect.right - viewRect.left);
  900.             
  901.             if ( max < 0 )
  902.             {
  903.                 // Deactivate the scrollbar.
  904.                 HiliteControl(_fHScrollbar, 255);
  905.             }
  906.             else
  907.             {
  908.                 HiliteControl(_fHScrollbar, 0);
  909.                 SetControlMaximum(_fHScrollbar, max);
  910.                 SetControlValue(_fHScrollbar, scrolledVals.h);
  911.             }
  912.         }
  913.             
  914.     SOM_CATCH_ALL
  915.     SOM_ENDTRY
  916. }
  917.  
  918. //------------------------------------------------------------------------------
  919. // Method:        CalcSelectionRgn
  920. // Origin:        TextEditor
  921. //
  922. // Description:    This method is called when a facet....
  923. //------------------------------------------------------------------------------
  924. #pragma segment TextEditorImaging
  925.  
  926. SOM_Scope    void
  927. SOMLINK        TextEditor__CalcSelectionRgn
  928.             (
  929.                 SampleCode_TextEditor*        somSelf,
  930.                 Environment*                ev,
  931.                 ODFacet*                    facet
  932.             )
  933. {
  934.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  935.     SOMMethodDebug("TextEditor","CalcSelectionRgn");
  936.  
  937.     SOM_TRY
  938.     
  939.         CFocus initiateDrawing(ev, facet);
  940.         
  941.         OpenRgn();
  942.         
  943.         _fTextension->GetSelectionHandler()->Draw();
  944.         
  945.         ODRgnHandle selectionRgn = ODNewRgn();
  946.         CloseRgn(selectionRgn);
  947.         
  948.         if ( selectionRgn )
  949.         {
  950.             ODDisposeHandle((ODHandle) _fSelectionRgn);
  951.             _fSelectionRgn = (Handle) selectionRgn;
  952.         }
  953.     
  954.     SOM_CATCH_ALL
  955.     SOM_ENDTRY
  956. }
  957.  
  958. //------------------------------------------------------------------------------
  959. // Method:        PresentationChanged
  960. // Origin:        ODPart
  961. //
  962. // Description:    This method is called when a facet....
  963. //------------------------------------------------------------------------------
  964. #pragma segment TextEditorImaging
  965.  
  966. SOM_Scope    void
  967. SOMLINK        TextEditor__PresentationChanged
  968.             (
  969.                 SampleCode_TextEditor*        somSelf, 
  970.                 Environment*                ev,
  971.                 ODFrame*                     frame
  972.             )
  973. {
  974.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  975.     SOMMethodDebug("TextEditor","PresentationChanged");
  976. }
  977.  
  978. //------------------------------------------------------------------------------
  979. // Method:        RepositionScrollbars
  980. // Origin:        TextEditor
  981. //
  982. // Description:    This method is called when a facet....
  983. //------------------------------------------------------------------------------
  984. #pragma segment TextEditorImaging
  985.  
  986. SOM_Scope    void
  987. SOMLINK        TextEditor__RepositionScrollbars
  988.             (
  989.                 SampleCode_TextEditor*        somSelf,
  990.                 Environment*                ev,
  991.                 ODFrame*                    frame
  992.             )
  993. {
  994.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  995.     SOMMethodDebug("TextEditor","RepositionScrollbars");
  996.  
  997.     SOM_TRY
  998.     
  999.         ODRect        bbox;
  1000.         Rect        frameRect;
  1001.         Rect        scrollBarRect;
  1002.         
  1003.         TempODShape frameShape = frame->AcquireFrameShape(ev, kODNULL);
  1004.         frameShape->GetBoundingBox(ev, &bbox);
  1005.     
  1006.         FixedToIntRect(bbox, frameRect);
  1007.             
  1008.         scrollBarRect = frameRect;
  1009.         scrollBarRect.left = scrollBarRect.right - 15;
  1010.         scrollBarRect.right += 1;
  1011.         scrollBarRect.top -= 1;
  1012.         scrollBarRect.bottom -= 14;
  1013.     
  1014.         MoveControl(_fVScrollbar, scrollBarRect.left, scrollBarRect.top);
  1015.         SizeControl(_fVScrollbar, 16, (scrollBarRect.bottom - scrollBarRect.top));
  1016.     
  1017.         scrollBarRect = frameRect;
  1018.         scrollBarRect.left -= 1;
  1019.         scrollBarRect.right -= 14;
  1020.         scrollBarRect.top = scrollBarRect.bottom - 15;
  1021.         scrollBarRect.bottom += 1;
  1022.     
  1023.         MoveControl(_fHScrollbar, scrollBarRect.left, scrollBarRect.top);
  1024.         SizeControl(_fHScrollbar, (scrollBarRect.right - scrollBarRect.left), 16);
  1025.         
  1026.         somSelf->AdjustScrollbars(ev, frame);
  1027.     
  1028.     SOM_CATCH_ALL
  1029.     SOM_ENDTRY
  1030. }
  1031.  
  1032. //------------------------------------------------------------------------------
  1033. // Method:        CanvasChanged
  1034. // Origin:        ODPart
  1035. //
  1036. // Description:    This method is called when a facet....
  1037. //------------------------------------------------------------------------------
  1038. #pragma segment TextEditorImaging
  1039.  
  1040. SOM_Scope    void
  1041. SOMLINK        TextEditor__CanvasChanged
  1042.             (
  1043.                 SampleCode_TextEditor*        somSelf, 
  1044.                 Environment*                ev,
  1045.                 ODFacet*                    facet
  1046.             )
  1047. {
  1048.     SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  1049.     SOMMethodDebug("TextEditor","CanvasChanged");
  1050. }
  1051.  
  1052. //------------------------------------------------------------------------------
  1053. // Method:        CanvasUpdated
  1054. // Origin:        ODPart
  1055. //
  1056. // Description:    This method is called when a facet....
  1057. //------------------------------------------------------------------------------
  1058. #pragma segment TextEditorImaging
  1059.  
  1060. SOM_Scope    void
  1061. SOMLINK        TextEditor__CanvasUpdated
  1062.             (
  1063.                 SampleCode_TextEditor*        somSelf, 
  1064.                 Environment*                ev,
  1065.                 ODCanvas*                    canvas
  1066.             )
  1067. {
  1068. //    SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  1069.     SOMMethodDebug("TextEditor","CanvasUpdated");
  1070. }
  1071.  
  1072. //------------------------------------------------------------------------------
  1073. // Method:        GetPrintResolution
  1074. // Origin:        ODPart
  1075. //
  1076. // Description:    This method is called when a facet....
  1077. //------------------------------------------------------------------------------
  1078. #pragma segment TextEditorImaging
  1079.  
  1080. SOM_Scope    ODULong
  1081. SOMLINK        TextEditor__GetPrintResolution
  1082.             (
  1083.                 SampleCode_TextEditor*        somSelf,
  1084.                 Environment*                ev,
  1085.                 ODFrame*                    frame
  1086.             )
  1087. {
  1088. //    SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  1089.     SOMMethodDebug("TextEditor","GetPrintResolution");
  1090.  
  1091.     return kMinImagingResolution;
  1092. }
  1093.  
  1094. //------------------------------------------------------------------------------
  1095. // Method:        AdjustBorderShape
  1096. // Origin:        ODPart
  1097. //
  1098. // Description:    This method is called by
  1099. //------------------------------------------------------------------------------
  1100. #pragma segment TextEditorLayout
  1101.  
  1102. SOM_Scope    ODShape*
  1103. SOMLINK        TextEditor__AdjustBorderShape
  1104.             (
  1105.                 SampleCode_TextEditor*        somSelf,
  1106.                 Environment*                ev,
  1107.                 ODFacet*                     embeddedFacet,
  1108.                 ODShape*                     shape
  1109.             )
  1110. {
  1111. //    SampleCode_TextEditorData *somThis = SampleCode_TextEditorGetData(somSelf);
  1112.     SOMMethodDebug("TextEditor","AdjustBorderShape");
  1113.  
  1114.     SOM_TRY
  1115.         ODAcquireObject(ev, shape);
  1116.     SOM_CATCH_ALL
  1117.         shape = kODNULL;
  1118.     SOM_ENDTRY
  1119.  
  1120.     return shape;
  1121. }
  1122.  
  1123.